package com.ruoyi.web.sso.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class CountSourceCodeRowUtil {
    private static int fileCount = 0;
    private static int fileLineCount = 0;
    private static int allLineCount = 0;
    private static int notesCount = 0;

    public static void countFileRows(File path) throws IOException {
        // 如果是目录，递归获取
        if (path.isDirectory()) {
            File[] files = path.listFiles();
            for (File file : files) {
                countFileRows(file);
            }
        }
        // 如果是文件
        if (path.isFile()) {
            // 判断文件类型
            String filePathName = path.getName();
            if (filePathName.endsWith(".java") ) {
//            if (true ) {
                fileLineCount = 0;
                fileCount++;
                String line;
                BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
                while ((line = bufferedReader.readLine()) != null) {
                    // 排除空白行和单行注释，多行注释未找到合适的正则表达式
                    if (line.matches("^\\s*$") || line.trim().matches("\\/\\/(.*)")) {
                        notesCount++;
                        continue;
                    }
                    fileLineCount++;
                    allLineCount++;
                }
                String[] filePaths = filePathName.split("\\\\");
                String fileName = filePaths[filePaths.length - 1];
//                System.out.println("[FileName]: " + fileName + ", [LineCounts]:" + fileLineCount);
                bufferedReader.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // 选择一个文件路径
        File file = new File("F:\\8\\jd-kfgl-boot");
//        File file = new File("F:\\6\\jd_zbgl-boot");

//        File file = new File("F:\\6\\jd-zbgl");
//        File file = new File("F:\\6\\jd-kfgl");
        countFileRows(file);
//        jd_kfgl-boot:[FileCount]:428, [allLineCount]:29251, [notesCount]:4237
        System.out.println("jd_kfgl-boot:[FileCount]:" + fileCount + ", [allLineCount]:" + allLineCount+", [notesCount]:" + notesCount);
//        System.out.println("jd_zbgl-boot：[FileCount]:" + fileCount + ", [allLineCount]:" + allLineCount+", [notesCount]:" + notesCount);
//        System.out.println("jd_zbgl：[FileCount]:" + fileCount + ", [allLineCount]:" + allLineCount+", [notesCount]:" + notesCount);
//        System.out.println("jd_kfgl：[FileCount]:" + fileCount + ", [allLineCount]:" + allLineCount+", [notesCount]:" + notesCount);

//        jd_kfgl-boot:[FileCount]:423, [allLineCount]:29192, [count]:4162
//        jd_zbgl-boot：[FileCount]:585, [allLineCount]:45866, [count]:6236
//        jd_zbgl：[FileCount]:362, [allLineCount]:122277, [count]:4095
//        jd_kfgl：[FileCount]:294, [allLineCount]:80707, [count]:2420
    }
}
